package com.stu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.commons.ServerResponse;
import com.stu.mapper.AddressMapper;
import com.stu.pojo.Address;
import com.stu.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;

    // 查询用户地址
    @Override
    public ServerResponse getAddressByUserId(Integer userId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Address> addressesList = addressMapper.selectList(queryWrapper);
        if (addressesList!=null)
            return ServerResponse.success("查询地址成功",addressesList);
        else
            return ServerResponse.fail("查询地址失败",null);
    }

    // 根据地址id查询地址
    @Override
    public Address getById(Integer addressId) {
        return addressMapper.selectById(addressId);
    }

    @Override
    public ServerResponse save(Address address) {
        int rows = addressMapper.insert(address);
        if(rows >0)
           return ServerResponse.success("添加地址成功",address);
        else
           return ServerResponse.fail("添加地址失败",null);
    }
}
